<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of canbo
 *
 * @author trung_000
 */
class canbo extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('m_sinhvien');
        $this->load->model('m_dmktkl');
        $this->load->model('m_odulieu');
        $this->load->model('m_bieumau');
        $this->load->model('m_canbo');
    }

    public function index() {
        $this->checkUser();

        $data['user'] = $this->session->userdata['user'];
        if ($data['user'] == FALSE) {
            $t = new user();
            $t->DangNhap();
        } else {
            $data['count'] = $this->m_don_sv->getCount(-1);
            $data['count0'] = $this->m_don_sv->getCount(0);
            $data['count4'] = $this->m_don_sv->getCount(4);
            $this->load->view('canbo/cb-index', $data);
        }
    }

    public function load_CB() {
        $this->checkUser();
        $data['count'] = $this->m_don_sv->getCount(-1);
        $data['count0'] = $this->m_don_sv->getCount(0);
        $data['dsdonduyet'] = $this->m_canbo->getAllDonduyet(0);
        $data['dsdonin'] = $this->m_canbo->getAllDonduyet(1);
        $data['user'] = $this->user;

        $data['dsmabieumau'] = $this->m_bieumau->getAllMa();
        $this->load->view('canbo/donduyet', $data);
        //print_r($data);
    }

    function xem_don_sinh_vien() {
        $this->UTF8();
        $data = $this->m_don_sv->getOneID2($this->input->get('Ma_Don_SV'));
        //  $this->showdata($data);
        $noidung = $data['donsv']['NoiDung'];
        foreach ($data['dsodulieu'] as $value) {
            if ($value['F'] == 1) {
                echo $value['Nhan'] . ' : ' . $noidung[$value['ViTriO']] . '<br>';
            }
        }
    }

    public function duyet2() {
        $this->checkUser();
        if (isset($_POST['bt1'])) {

            $data = $this->m_don_sv->getOneID2($this->input->post('Ma_Don_SV'));
            //  $this->showdata($data);
            $data['user'] = $this->user;
            $this->load->view('canbo/xemnoidungdon', $data);
        }
    }

    function xemdon2() {
        $data = $this->m_don_sv->getOneID2($this->input->get('Ma_Don_SV'));

        $this->load->view('canbo/NoiDungDon', $data);
    }

    function xemdon() {
        $this->checkUser();

        switch ($_POST['bt']) {
            case 1:
                $data = $this->m_don_sv->getOneID2($this->input->post('Ma_Don_SV'));
                $this->load->view('canbo/NoiDungDon', $data);
                break;
            case 2:


                $data = $this->m_don_sv->getOneID2($_POST['Ma_Don_SV']);
                $data['user'] = $this->user;
                //  common::showdata($data);
                $this->load->view('canbo/capnhattradon', $data);
                break;

            default:

                break;
        }
    }

    public function duyet() {
        $this->checkUser();
        if (isset($_POST['bt1'])) {
            $maSV = $this->input->get_post('Ma_Don_SV');
            $data['svform'] = $this->m_canbo->getAllSinhvien($maSV);
            $this->load->view('donsv/chungnhan_sv', $data);
        }
    }

    function xuly_duyetdon() {

        switch ($_POST['bt']) {
            case 1:
                $Ma_Don_SV = $this->input->get_post('Ma_Don_SV');
                $NoiDung_TT = 'Đơn đã duyệt, đang chờ lấy dấu';
                $this->m_don_sv->capnhattrangthai($Ma_Don_SV, $NoiDung_TT, 1);
                $this->load_CB();

                break;
            case 2:
                $Ma_Don_SV = $this->input->get_post('Ma_Don_SV');
                $NoiDung_TT = $this->input->get_post('NoiDung');
                $this->m_don_sv->capnhattrangthai($Ma_Don_SV, $NoiDung_TT, -1);
                $this->load_CB();

                break;

            default:
                break;
        }
    }

    public function updatestt() {
        if (isset($_POST['duyet'])) {
            $ma_donSV = $this->input->get_post('Ma_Don_SV');
            $data = $this->m_canbo->getUpdateDon($ma_donSV);
            $this->load_CB();
        } else if (isset($_POST['submit1'])) {
            $ma_donSV = $this->input->get_post('Ma_Don_SV');
            $this->m_canbo->getUpdate2($ma_donSV);
            $this->load_CB();
        }
    }

    public function xuly_form_duyet() {
        $this->checkUser();
        print_r($_POST);
    }

    public function page_lapdanhsachkhenthuong() {
        $this->checkUser();

//        $data['dsdmkt'] = $this->m_dmktkl->getAll(1);
//        $data['dssv'] = $this->m_sinhvien->getAll();
        // print_r($data);
//        $this->load->view('canbo/page_lapdanhsachkhenthuong', $data);
        $data['svkt'] = $this->m_canbo->getSvKTKL();
        $data['user'] = $this->user;
        $this->load->view('canbo/KTKL', $data);
    }

    public function page_thembieumau() {
        $this->checkUser();

        $data['Ma_BieuMau'] = $this->m_bieumau->getID();
        $data['dsbieumau'] = $this->m_bieumau->getAll(100);
        $this->load->view('canbo/page_thembieumau', $data);
    }

    public function xuly_thembieumau() {


        $this->checkUser();
        $this->m_bieumau->them($_FILES, $this->input->get_post('Ma_BieuMau'), $this->input->get_post('TenBieuMau'), $this->input->get_post('MoTa'), $this->input->get_post('Loai'));

// loai 1
        if ($this->input->get_post('Nhan'))
            for ($i = 0; $i < count($_POST['Nhan']); $i++) {
                $this->m_odulieu->them($_POST['ViTriO'][$i], $_POST['Nhan'][$i], $_POST['Ma_BieuMau'], $_POST['type'][$i], 1);
            }
// loai 2
        if ($this->input->get_post('Nhan1'))
            for ($i = 0; $i < count($_POST['Nhan1']); $i++) {
                $this->m_odulieu->them($_POST['ViTriO1'][$i], $_POST['Nhan1'][$i], $_POST['Ma_BieuMau'], 20, 0);
            }

        $this->page_thembieumau();
    }

    private $user = '';

    public function checkUser() {
        $this->user = $this->session->userdata['user'];
    }

    function xuly_dsbieumau() {
        switch ($_POST['bt']) {
            case 0:

                $this->m_bieumau->xoa($_POST['Ma_BieuMau']);
                $this->page_thembieumau();
                break;
            case 1:
                $data = $this->m_bieumau->getOneId($_POST['Ma_BieuMau']);
                $data['dsinput'] = $this->m_bieumau->getODuLieuId($_POST['Ma_BieuMau']);
                // common::showdata($data);
                $this->load->view('canbo/page_suabieumau', $data);

                break;
            case 2:
                // print_r($_POST);
                $this->m_bieumau->duyet($_POST['Ma_BieuMau'], $_POST['F']);
                $this->page_thembieumau();
            //exit();

            default:
                break;
        }
    }

    function xuly_suabieumau() {
        // common::showdata($_POST);
        switch ($_POST['bt']) {
            case 1:
                $this->m_bieumau->capnhat($_POST['Ma_BieuMau'], $_POST['TenBieuMau'], $_POST['MoTa'], $_POST['Loai']);
                // câp nhập
                if (is_array($this->input->get_post('id'))) {
                    foreach ($_POST['id'] as $key => $value) {
                        $this->m_odulieu->capnhat($value, $_POST['Nhan'][$key], $_POST['ViTriO'][$key], $_POST['type'][$key], 0);
                    }
                }
                // thêm ô dư liệu
                if (is_array($this->input->get_post('them'))) {
                    foreach ($_POST['them'] as $key => $value) {
                        if ($value == 1) {
                            $this->m_odulieu->them($_POST['ViTriO'][$key], $_POST['Nhan'][$key], $_POST['Ma_BieuMau'], $_POST['type'][$key], $_POST['F'][$key]);
                        }
                    }
                }

                break;

                break;

            default:
                break;
        }
        $this->page_thembieumau();
    }

    function xuly_lapdanhsachkhenthuong() {
        echo '<pre>';
        print_r($_POST);
        echo '</pre>';
    }

    function page_diemrenluyen() {
        $this->checkUser();
        $data['user'] = $this->user;
        $data['dstudanhgia'] = $this->m_diemrenluyen->getAll();
        $this->load->view('canbo/duyet_diem_ren_luyen', $data);
    }

    function xuly_duyetdiemnluyen() {
        $this->checkUser();
        switch ($_POST['bt']) {
            case 1:

                $data = $this->m_diemrenluyen->getOneId($_POST['id_diemrenluyen']);
                $data['user'] = $this->user;
                $data['bt'] = 1;
                $this->load->view('canbo/xac_nhan_diem_ren_luyen', $data);
                break;
            case 2:
                $data = $this->m_diemrenluyen->getOneId($_POST['id_diemrenluyen']);
                $data['user'] = $this->user;

                $this->showdata($_POST);
                $this->load->view('canbo/xac_nhan_diem_ren_luyen', $data);

                break;


            default:
                break;
        }
    }

    function xacnhandienrenluyen() {
        switch ($_POST['bt']) {
            case 1:


                break;
            case 2:
                $this->m_diemrenluyen->capnhat($_POST);
//$this->showdata($_POST);
                $this->page_diemrenluyen();

                break;

            default:
                break;
        }
    }

    function UTF8() {
        echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
    }

    function showdata($data) {
        echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
        echo '<pre>';
        print_r($data);
        echo '</pre>';
        exit();
    }

    function page_quanlydonsv() {
        $this->checkUser();
        $data['dsdonsv'] = $this->m_don_sv->getLike('');
        $data['user'] = $this->user;
        //$this->showdata($data);
        $this->load->view('canbo/quan_ly_don_sv', $data);
    }

    function ajax_donsv() {

        $data['dsdonsv'] = $this->m_don_sv->getLike($this->input->get('key'));
        $this->UTF8();
        $this->load->view('canbo/tb-donsv', $data);
    }

    function ajax_dssv() {

        $data['dssinhvien'] = $this->m_sinhvien->getLike($this->input->get('key'));
        $this->UTF8();
        $data['dm_ktkl'] = $this->m_dmktkl->getOneID($this->input->get('Ma_dmKLKT'));

        $this->load->view('canbo/tb-themsinhvienklkt', $data);
    }

    function page_kyluat() {
        $this->checkUser();
        $data['user'] = $this->user;
        $data['dmKL'] = $this->m_dmktkl->getAll(100);
        $this->load->view('canbo/page_kyluat', $data);
    }

    function xuly_kyluat() {
        $this->showdata($_POST);
    }

    function lapdskl() {
        common::showdata($_POST);
    }

    function xuly_tradon() {
        $noidung = 'Đơn đã được trả';
        $noidung.='<br>Người nhận: ' . $_POST['NguoiNhan'] . '<br>Ghi chú: ' . $_POST['GhiChu'].'<br>'.date('Y-m-d H:i:s');
        $this->m_don_sv->capnhattrangthai($_POST['Ma_Don_SV'], $noidung, -2);
        $this->page_quanlydonsv();
    }

    function intatdon() {

        $mabieumau = $this->input->get_post('Ma_BieuMau');
        $d = $this->m_don_sv->getDatain($mabieumau);
        foreach ($d as $key => $value) {
            $noidung = 'Đơn đã được đóng dấu, mời bạn đến phòng công tác sinh viên nhân đơn ';
           
            $this->m_don_sv->capnhattrangthai($value['Ma_Don_SV'], $noidung, 2);
            
        }
        Excel::xuatfile2($d);
    }

    function ajax_xoa() {
        $id = $this->input->get_post('id');
        $this->m_odulieu->xoa($id);
        echo 'Xóa';
    }

}
